export const useColorStore = defineStore("color", () => {
  // =========> 主题色修改
  const mainColor = ref(localStorage.getItem('mainColor') || '#ff4000')

  const colourBlend = (c1, c2, ratio) => {
    ratio = Math.max(Math.min(Number(ratio), 1), 0)
    let r1 = parseInt(c1.substring(1, 3), 16)
    let g1 = parseInt(c1.substring(3, 5), 16)
    let b1 = parseInt(c1.substring(5, 7), 16)
    let r2 = parseInt(c2.substring(1, 3), 16)
    let g2 = parseInt(c2.substring(3, 5), 16)
    let b2 = parseInt(c2.substring(5, 7), 16)
    let r = Math.round(r1 * (1 - ratio) + r2 * ratio)
    let g = Math.round(g1 * (1 - ratio) + g2 * ratio)
    let b = Math.round(b1 * (1 - ratio) + b2 * ratio)
    r = ('0' + (r || 0).toString(16)).slice(-2)
    g = ('0' + (g || 0).toString(16)).slice(-2)
    b = ('0' + (b || 0).toString(16)).slice(-2)
    return '#' + r + g + b
  }

  // 初始化时立即应用颜色
  const applySavedColor = (color) => {
    const el = document.documentElement;
    const body = document.querySelector("body");
    el.style.setProperty("--el-color-primary", color);
    let mixColor = "#ffffff";
    for (let i = 1; i < 10; i++) {
      el.style.setProperty(`--el-color-primary-light-${i}`, colourBlend(color, mixColor, i / 10));
      el.style.setProperty(`--el-color-primary-dark-${i}`, colourBlend(color, mixColor, i / 10));
    }
    el.style.setProperty(`--el-color-primary-dark-2`, colourBlend(color, mixColor, 0.2));
  }

  // 页面加载时立即执行一次
  applySavedColor(mainColor.value)

  const changeColor = (color = mainColor.value) => {
    mainColor.value = color // 更新 ref 的值
    applySavedColor(mainColor.value)
    localStorage.setItem('mainColor', color)
  }

  // <============


  return { mainColor, changeColor }
})